/*
 * Copyright (c) 2007 OpenNETCF Consulting, LLC
 * 
 * OpenNETCF MITx11 License
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial 
 * portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

using OpenNETCF.Wia;

namespace WIATest
{
    public partial class Form1 : Form
    {
        CameraCollection m_cameras;
 
        public Form1()
        {
            InitializeComponent();
            m_cameras = CameraCollection.GetCameras();
            m_cameras.CameraAdded += new OnConnect(m_cameras_CameraAdded);
            m_cameras.CameraRemoved += new OnDisconnect(m_cameras_CameraRemoved);
            RefreshCameraList();
        }

        void m_cameras_CameraRemoved(Camera camera)
        {
            foreach (TabPage tab in cameraTabs.TabPages)
            {
                if (tab.Tag.Equals(camera))
                {
                    cameraTabs.TabPages.Remove(tab);
                    return;
                }
            }
        }

        void m_cameras_CameraAdded(Camera camera)
        {
            System.Threading.Thread.Sleep(3000);

            TabPage page = new TabPage(camera.Name);
            cameraTabs.TabPages.Add(page);
            page.Tag = camera;
            camera.DisplayWindow = page;

            if (autoSelect.Checked)
            {
                cameraTabs.SelectedTab = page;
            }
        }

        private void RefreshCameraList()
        {
            cameraTabs.TabPages.Clear();

            foreach (Camera cam in m_cameras)
            {
                TabPage page = new TabPage(cam.Name);
                cameraTabs.TabPages.Add(page);
                page.Tag = cam;
                cam.DisplayWindow = page;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            picture.Image = ((Camera)cameraTabs.SelectedTab.Tag).GrabFrame();
        }

        void SetPicture()
        {
        }

        private void play_Click(object sender, EventArgs e)
        {
            ((Camera)cameraTabs.SelectedTab.Tag).Play();
        }

        private void pause_Click(object sender, EventArgs e)
        {
            ((Camera)cameraTabs.SelectedTab.Tag).Pause();
        }
    }
}